package org.sky.wall.visual.ui

import android.annotation.SuppressLint
import android.app.WallpaperManager
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import org.sky.wall.visual.MainActivity
import org.sky.wall.visual.R
import org.sky.wall.visual.databinding.ActivitySettingBinding
import org.sky.wall.visual.databinding.ActivityVideoBinding
import java.io.IOException

class SettingActivity : AppCompatActivity() {
	private lateinit var binding: ActivitySettingBinding
	private var id = R.drawable.a1
	private var index = 0
	
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		binding = ActivitySettingBinding.inflate(layoutInflater)
		setContentView(binding.root)
		setWallpaper()
		getData()
	}
	
	private fun getData() {
		id = intent.getIntExtra("url", R.drawable.a1)
		binding.img.setImageResource(id)
	}
	
	private fun setWallpaper() {
		binding.setting.setOnClickListener {
			showWallpaperOptionsDialog()
		}
		index = intent.getIntExtra("index", 0)
	}
	
	private fun showToast(message: String) {
		runOnUiThread {
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
			startActivity(Intent(this, MainActivity::class.java))
			finish()
		}
	}
	
	@SuppressLint("ResourceType")
	private fun showWallpaperOptionsDialog() {
		val builder: AlertDialog.Builder = AlertDialog.Builder(this)
		
		// Inflate custom layout
		val inflater = layoutInflater
		val customLayout = inflater.inflate(R.layout.setting_layout, null)
		
		builder.setView(customLayout)
		
		val wallpaperManager = WallpaperManager.getInstance(applicationContext)
		
		val bitmapDrawable = binding.img.drawable as? BitmapDrawable
		val bitmap = bitmapDrawable?.bitmap
		
		builder.setCancelable(true)
		
		val dialog: AlertDialog = builder.create()
		dialog.window?.setGravity(Gravity.BOTTOM)
		
		// Access the views in your custom layout
		val wholeOption = customLayout.findViewById<TextView>(R.id.whole)
		val desktopOption = customLayout.findViewById<TextView>(R.id.desktop)
		val lockOption = customLayout.findViewById<TextView>(R.id.lock)
		
		lockOption.setOnClickListener {
			// Handle Whole option in a background thread
			dialog.dismiss()
			Thread {
				setWallpaperWithFlag(wallpaperManager, bitmap, WallpaperManager.FLAG_LOCK)
				runOnUiThread {
					showToast("Successfully set")
				}
			}.start()
		}
		
		desktopOption.setOnClickListener {
			dialog.dismiss()
			// Handle Desktop option in a background thread
			Thread {
				setWallpaperWithFlag(wallpaperManager, bitmap, WallpaperManager.FLAG_SYSTEM)
				runOnUiThread {
					showToast("Successfully set")
				}
			}.start()
		}
		
		wholeOption.setOnClickListener {
			// Handle Lockscreen option in a background thread
			Thread {
				try {
					wallpaperManager.setResource(id)
					dialog.dismiss()
					bitmap?.let {
						wallpaperManager.setBitmap(it, null, true, WallpaperManager.FLAG_LOCK or WallpaperManager.FLAG_SYSTEM)
					}
					runOnUiThread {
						showToast("Successfully set")
					}
				} catch (e: IOException) {
					e.printStackTrace()
				}
			}.start()
		}
		
		dialog.show()
	}

	
	
	private fun setWallpaperWithFlag(
			wallpaperManager: WallpaperManager,
			bitmap: Bitmap?,
			flag: Int
	) {
		bitmap?.let {
			try {
				wallpaperManager.setBitmap(it, null, true, flag)
			} catch (e: IOException) {
				e.printStackTrace()
			}
		}
	}
}

